#include "CubeEmitter.h"
#include "TextureLoader.h"
#include <cstdlib>
#include <ctime>

int CubeEmitter::randomInt(int max)
{
	return rand() % max;
}

void CubeEmitter::emitTo(CubePileGrid * cubePiles)
{
    if (emissionsLeft > 0)
    {
        int x = randomInt(GRID_SIZE);
		int z = randomInt(GRID_SIZE);
		CubePile * cubePile = cubePiles->at(x,z);
        
		if(cubePile->size() < MAX_CUBES_IN_PILE)
		{
		
		    emit(x, z, cubePile);
		    
	        emissionsLeft--;	
		    
		    if(emissionsLeft == 0)
		    {
//		    	cout << "no more cubes to emit" << "\n";
		    }
		}
    }
	
}

void CubeEmitter::emit(int x, int z, CubePile * cubePile)
{
	int randomTexture = randomInt(NUM_CUBE_TEXTURES);
	Cube * cube = new Cube(textureLoader, randomTexture);
	cube->xPos = x* (2 + CUBE_GAP);
	cube->yPos = CUBE_Y_DROP;
	cube->zPos = z* (2 + CUBE_GAP);
	cubePile->dropCube(cube);
}


bool CubeEmitter::isFinishedEmitting()
{
	return emissionsLeft == 0;
}

CubeEmitter::CubeEmitter(CubePileGrid * aCubePiles, TextureLoader * aTextureLoader, int aMaxEmissions)
	: RandomTimeEmitter(aCubePiles, MIN_EMIT_TIME, MAX_EMIT_TIME)
{
	textureLoader = aTextureLoader;
    emissionsLeft = aMaxEmissions;
}
